<?php
class RecipesController extends AppController{
	public $uses=array('Recipe');

	public $layout='design';

	public function index(){
		$recipeData=$this->Recipe->find('all');
		$this->set('recipeData',$recipeData);
	}

	public function edit($id=null){
		if($this->request->isPost()||$this->request->isPut()){
			if(!empty($this->data)){
				if($this->Recipe->save($this->data)){
					$this->Session->setFlash("Succeed");
					$this->redirect(array('action'=>'index'));
					return;
				}
			}
			$this->Session->setFlash('Faild');
		}else{
			if(!is_null($id)){
				$this->data=$this->Recipe->findById($id);
			}
		}

		$isEdit=false;

		if(!is_null($id)){
			$isEdit=true;
		}

		$this->set('isEdit',$isEdit);
	}

	public function add(){
		$this->edit();
		$this->render('edit');
	}

	public function delete($id){
		if($this->request->isDelete()){
			if(!empty($this->data)){
				if($this->Recipe->delete($this->data[$this->Recipe->alias]['id'])){
					$this->Session->setFlash('Succeed');
					$this->redirect(array('action'=>'index'));
					return;
				}
			}

			$this->Session->setFlash('Faild');
			$this->redirect(array('action'=>'index'));
			return;
		}else{
			$this->data=$this->Recipe->findById($id);
			$this->set('data',$this->data);
			if(empty($this->data)){
				$this->Session->setFlash('Data not found');
				$this->redirect(array('action'=>'index'));
				return;
			}
		}
	}

	public function detail($id){
		$this->data=$this->Recipe->findById($id);
		$this->set('data',$this->data);
	}
}